package com.lfz.quality_effect.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * @author codermy
 * @createTime 2020/7/10
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    private static final String DEFAULT_PATH = "/api";

    @Bean
    public Docket createRestApi() {

        //添加head参数end
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .enable(true)//是否开启
                .select()
                //为当前包路径
                .apis(scanBasePackage("com.lfz.quality_effect.controller"))
                .paths(PathSelectors.regex("^(?!login).*$"))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                .apiInfo(webApiInfo());
    }
    public static Predicate scanBasePackage(final String basePackage) {
        if(StringUtils.isBlank(basePackage))

            throw new NullPointerException("basePackage不能为空，多个包扫描使用;分隔");

        String[] controllerPack = basePackage.split(";");

        Predicate predicate = null;

        for (int i = controllerPack.length -1; i >= 0 ; i--) {
            String strBasePackage = controllerPack[i];

            if(StringUtils.isNotBlank(strBasePackage)){
                Predicate secPredicate = RequestHandlerSelectors.basePackage(strBasePackage);

                predicate = predicate == null ? secPredicate : Predicates.or(secPredicate,predicate);

            }

        }

        if(predicate == null)

            throw new NullPointerException("basePackage配置不正确，多个包扫描使用;分隔");

        return predicate;

    }

    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("Authorization", "Authorization", "header"));
    }

    private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!login).*$"))
                        .build()
        );
    }
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(
                new SecurityReference("Authorization", authorizationScopes));
    }
    /**
     * 该套 API 说明，包含作者、简介、版本、等信息
     * @return
     */
    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .contact(new Contact("CoderMy",null,null))
                .title("零辅助调解系统API文档")
                .description("本文档描述了零辅助调解系统接口定义")
                .version("1.0.8")
                .build();
    }

}